package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.CardManaApplicationException;
import com.turing.post.cardmanagement.util.MakeCardActionDetail;
import com.turing.post.cardmanagement.util.MakeCardActionDetailDao;
import com.turing.post.cardmanagement.util.MakeCardApproval;
import com.turing.post.cardmanagement.util.MakeCardApprovalBusiness;
import com.turing.post.cardmanagement.util.MakeCardApprovalDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOpUser;
import com.turing.post.common.util.SysOpUserDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueBranch;
import com.turing.post.organization.util.IssueBranchDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 待申请制卡业务实现
 * 
 * @author YangShuo
 * 
 */
@Stateless(mappedName = "cardmanagement.impl.MakeCardApprovalBusinessImpl")
@Remote
@Local
public class MakeCardApprovalBusinessImpl implements MakeCardApprovalBusiness {

	@EJB(mappedName = "organization.impl.IssueBranchDaoImpl")
	private IssueBranchDao IssueBranchDaoImpl;

	@EJB(mappedName = "common.impl.SysOpUserDaoImpl")
	private SysOpUserDao SysOpUserDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao PCPSCommonDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.MakeCardApprovalDaoImpl")
	private MakeCardApprovalDao MakeCardApprovalDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.MakeCardActionDetailDaoImpl")
	private MakeCardActionDetailDao MakeCardActionDetailDaoImpl;

	// 创建制卡申请
	@Override
	public BigInteger creatMakeCardApproval(PCPSWebInput webInput,
			MakeCardApproval mac) throws PCPSApplicationException {
		// 制卡原因非空
		if (mac.getReason() == null)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"未提交制卡原因");
		// 有效期不可<=0
		if (mac.getExpired().intValue() <= 0)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"有效期异常");
		// 根据操作员号取得操作员信息，对比branchno，验证是否发卡商，操作员是否此发卡商机构所属
		SysOpUser oper = SysOpUserDaoImpl.getSysOpUserByOperId(mac.getOperId());
		String issueBranchNo = oper.getOperBranchNo();
		if (mac.getCardCount() > 10000) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"制卡数量最大10000张！");
		}
		mac.setIssueBranchNo(issueBranchNo);
		if (!oper.getOperBranchNo().equals(issueBranchNo)) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"操作员不属于此发卡商机构");
		}
		if (PCPSUtil.branchDistinguish(issueBranchNo) != PCPSUtil.SysBranch_IssueBranch) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"操作员所在不是发卡商机构");
		}
		IssueBranch issbrch = IssueBranchDaoImpl
				.getIssueBranchByIssueBranchNo(issueBranchNo);
		if (issbrch == null
				|| issbrch.getStatus() == IssueBranch.ISSUEBRANCH_STATUS_NOTACTIVATED)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"操作员所在机构不存在或被锁定");
		// CardClassic cc = CardClassicDaoImpl.getCardClassicByCardClassicId(mac
		// .getCardClassicId());
		// 检查卡种类是否被锁定
		// if (cc.getStatus() == CardClassic.CARDCLASSIC_STATUS_STOP)
		// throw new CardManaApplicationException(
		// PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
		// "此种卡类已失效");
		// 为带发卡时
		// if
		// (cc.getProxyIssue().equals(CardClassic.CARDCLASSIC_PROXYISSUE_YES)) {
		// if (PCPSUtil.isEmpty(cc.getProxyBin())
		// || cc.getProxyBin().trim().length() != 6) {
		// throw new CardManaApplicationException(
		// PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
		// "", "代发卡Bin为空或格式错误，必须6位");
		// }
		// }
		// 根据发卡商机构号取得发卡商
		mac.setIssueCompanyNo(PCPSUtil.getICNoByBranchNo(issueBranchNo));
		Date now = PCPSCommonDaoImpl.getDatabaseSysTime();
		// 取当前时间为申请时间
		mac.setApplyForTime(now);
		BigInteger makeCardApplyForId = PCPSCommonDaoImpl.getUniqueIntegerKey();
		// 取唯一大整数ID
		mac.setMakeCardApplyForId(makeCardApplyForId);
		// 状态为待审批
		mac.setStatus(MakeCardApproval.STATUS_APPROVAL);
		// 插入记录
		MakeCardApprovalDaoImpl.insertMakeCardApproval(mac);
		// 插入明细
		insertMakeCardActionDetail(makeCardApplyForId, webInput.getOperId(),
				MakeCardActionDetail.MAKECARDACTIONDETAIL_APPLY,
				MakeCardApproval.STATUS_APPROVAL,
				MakeCardApproval.STATUS_APPROVAL, "创建制卡申请单");
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "创建制卡申请", "InitialInStockOrderId:"
						+ mac.getMakeCardApplyForId());
		return mac.getMakeCardApplyForId();
	}

	@SuppressWarnings("unused")
	private void insertMakeCardActionDetail(BigInteger id, BigInteger operId,
			String actionType, String start, String end, String remark)
			throws PCPSApplicationException {
		// 插入一条状态迁移明细
		MakeCardActionDetail makeCardActionDetail = new MakeCardActionDetail();
		makeCardActionDetail.setMakeCardApplyForId(id);
		makeCardActionDetail.setActionTime(PCPSCommonDaoImpl
				.getDatabaseSysTime());
		makeCardActionDetail.setOperId(operId);
		makeCardActionDetail.setActionType(actionType);
		makeCardActionDetail.setInitialTranStatus(start);
		makeCardActionDetail.setEndTranStatus(end);
		makeCardActionDetail.setRemark(remark);
		MakeCardActionDetailDaoImpl
				.insertMakeCardActionDetail(makeCardActionDetail);
	}

	/**
	 * 业务审批制卡申请 ，业务同意
	 * 
	 * @param MakeCardApplyForId
	 * @param AuthOperNo
	 * 
	 */
	@Override
	public void approvalMakeCardApprovalBySale(PCPSWebInput webInput,
			BigInteger MakeCardApplyForId, BigInteger AuthOperNo)
			throws PCPSApplicationException {
		// 根据id取得已提交的信息
		MakeCardApproval mca = MakeCardApprovalDaoImpl
				.getMakeCardApprovalByMakeCardApplyForIdForUpdate(MakeCardApplyForId);
		if (!mca.getStatus().equals(MakeCardApproval.STATUS_APPROVAL))
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"状态不是等待审批");
		// 验证审批人是否同一机构
		if (!(SysOpUserDaoImpl.getSysOpUserByOperId(AuthOperNo)
				.getOperBranchNo()).equals(mca.getIssueBranchNo().trim())) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"审批人不属于此机构");
		}
		// 检查机构是否激活
		if (IssueBranchDaoImpl.getIssueBranchByIssueBranchNo(
				mca.getIssueBranchNo().trim()).getStatus() == IssueBranch.ISSUEBRANCH_STATUS_NOTACTIVATED)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"此机构被锁定");
		// 检查审批人不能和申请人是同一个人
		if (mca.getOperId().equals(AuthOperNo)) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"审批人和申请人是同一人!");
		}
		// 赋值后提交审批结果
		mca.setStatus(MakeCardApproval.STATUS_SALESAUTH);
		MakeCardApprovalDaoImpl.updateMakeCardApproval(mca);
		// 插入一条状态迁移明细
		MakeCardActionDetail makeCardActionDetail = new MakeCardActionDetail();
		makeCardActionDetail.setMakeCardApplyForId(mca.getMakeCardApplyForId());
		makeCardActionDetail.setActionTime(PCPSCommonDaoImpl
				.getDatabaseSysTime());
		makeCardActionDetail.setOperId(AuthOperNo);
		makeCardActionDetail
				.setActionType(MakeCardActionDetail.MAKECARDACTIONDETAIL_ACTIONTYPE_FIRSTTRIALPASS);
		makeCardActionDetail
				.setEndTranStatus(MakeCardApproval.STATUS_SALESAUTH);
		makeCardActionDetail
				.setInitialTranStatus(MakeCardApproval.STATUS_APPROVAL);
		makeCardActionDetail.setRemark("业务审批通过");

		MakeCardActionDetailDaoImpl
				.insertMakeCardActionDetail(makeCardActionDetail);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "制卡申请业务同意", "InitialInStockOrderId:"
						+ makeCardActionDetail.getMakeCardApplyForId());
	}

	/**
	 * 业务审批制卡申请 ，业务拒绝
	 * 
	 * @param MakeCardApplyForId
	 * @param RejectedOperNo
	 * @param RejectedReason
	 */
	@Override
	public void rejecteMakeCardApprovalBySale(PCPSWebInput webInput,
			BigInteger MakeCardApplyForId, BigInteger RejectedOperNo,
			String RejectedReason) throws PCPSApplicationException {
		// 根据id取得已提交的信息
		MakeCardApproval mca = MakeCardApprovalDaoImpl
				.getMakeCardApprovalByMakeCardApplyForIdForUpdate(MakeCardApplyForId);
//		System.out.println("==============" + mca.getStatus()
//				+ "===================" + MakeCardApplyForId);
		if (!mca.getStatus().equals(MakeCardApproval.STATUS_APPROVAL))
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"状态不是等待审批");
		// 验证审批人是否同一机构
		if (!(SysOpUserDaoImpl.getSysOpUserByOperId(RejectedOperNo))
				.getOperBranchNo().equals(mca.getIssueBranchNo())) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"审批人不属于此机构");
		}

		// 检查机构是否激活
		if (IssueBranchDaoImpl.getIssueBranchByIssueBranchNo(
				mca.getIssueBranchNo()).getStatus() == IssueBranch.ISSUEBRANCH_STATUS_NOTACTIVATED)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"此机构被锁定");

		if (RejectedReason == null || RejectedReason.equals(""))
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"拒绝原因不可为空");

		// 赋值后提交审批结果
		mca.setStatus(MakeCardApproval.STATUS_REJECT);// 状态
		MakeCardApprovalDaoImpl.updateMakeCardApproval(mca);
		// 插入一条状态迁移明细
		MakeCardActionDetail makeCardActionDetail = new MakeCardActionDetail();
		makeCardActionDetail.setMakeCardApplyForId(mca.getMakeCardApplyForId());
		makeCardActionDetail.setActionTime(PCPSCommonDaoImpl
				.getDatabaseSysTime());
		makeCardActionDetail.setOperId(RejectedOperNo);
		makeCardActionDetail
				.setActionType(MakeCardActionDetail.MAKECARDACTIONDETAIL_ACTIONTYPE_FIRSTTRIALNOPASS);
		makeCardActionDetail
				.setInitialTranStatus(MakeCardApproval.STATUS_APPROVAL);
		makeCardActionDetail.setEndTranStatus(MakeCardApproval.STATUS_REJECT);
		makeCardActionDetail.setRemark(RejectedReason);
		MakeCardActionDetailDaoImpl
				.insertMakeCardActionDetail(makeCardActionDetail);

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "制卡申请业务拒绝", "InitialInStockOrderId:"
						+ makeCardActionDetail.getMakeCardApplyForId());
	}

	/**
	 * 财务审批制卡申请 ，财务同意
	 * 
	 * @param MakeCardApplyForId
	 * @param AuthOperNo
	 * 
	 */
	@Override
	public void approvalMakeCardApprovalByFINANCE(PCPSWebInput webInput,
			BigInteger MakeCardApplyForId, BigInteger AuthOperNo)
			throws PCPSApplicationException {
		// 根据id取得已提交的信息
		MakeCardApproval mca = MakeCardApprovalDaoImpl
				.getMakeCardApprovalByMakeCardApplyForIdForUpdate(MakeCardApplyForId);
		if (!mca.getStatus().equals(MakeCardApproval.STATUS_SALESAUTH))
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"状态不是业务批准");
		// 验证审批人是否同一机构
		if (!(SysOpUserDaoImpl.getSysOpUserByOperId(AuthOperNo)
				.getOperBranchNo()).equals(mca.getIssueBranchNo().trim())) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"审批人不属于此机构");
		}
		// 检查机构是否激活
		if (IssueBranchDaoImpl.getIssueBranchByIssueBranchNo(
				mca.getIssueBranchNo().trim()).getStatus() == IssueBranch.ISSUEBRANCH_STATUS_NOTACTIVATED)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"此机构被锁定");
		// 检查审批人不能和申请人是同一个人
		if (mca.getOperId().equals(AuthOperNo)) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"审批人和申请人是同一人!");
		}
		// 赋值后提交审批结果
		mca.setStatus(MakeCardApproval.STATUS_FINANCEAUTH);
		MakeCardApprovalDaoImpl.updateMakeCardApproval(mca);
		// 插入一条状态迁移明细
		MakeCardActionDetail makeCardActionDetail = new MakeCardActionDetail();
		makeCardActionDetail.setMakeCardApplyForId(mca.getMakeCardApplyForId());
		makeCardActionDetail.setActionTime(PCPSCommonDaoImpl
				.getDatabaseSysTime());
		makeCardActionDetail.setOperId(AuthOperNo);
		makeCardActionDetail
				.setActionType(MakeCardActionDetail.MAKECARDACTIONDETAIL_ACTIONTYPE_FINALJUDGMENPASS);
		makeCardActionDetail
				.setEndTranStatus(MakeCardApproval.STATUS_FINANCEAUTH);
		makeCardActionDetail
				.setInitialTranStatus(MakeCardApproval.STATUS_SALESAUTH);
		makeCardActionDetail.setRemark("财务审批通过");
		MakeCardActionDetailDaoImpl
				.insertMakeCardActionDetail(makeCardActionDetail);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "制卡申请财务同意", "InitialInStockOrderId:"
						+ makeCardActionDetail.getMakeCardApplyForId());
	}

	/**
	 * 财务审批制卡申请 ，财务拒绝
	 * 
	 * @param MakeCardApplyForId
	 * @param RejectedOperNo
	 * @param RejectedReason
	 */
	@Override
	public void rejecteMakeCardApprovalByFINANCE(PCPSWebInput webInput,
			BigInteger MakeCardApplyForId, BigInteger RejectedOperNo,
			String RejectedReason) throws PCPSApplicationException {
		// 根据id取得已提交的信息
		MakeCardApproval mca = MakeCardApprovalDaoImpl
				.getMakeCardApprovalByMakeCardApplyForIdForUpdate(MakeCardApplyForId);
		if (!mca.getStatus().equals(MakeCardApproval.STATUS_SALESAUTH))
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"状态不是业务批准");

		// 验证审批人是否同一机构
		if (!(SysOpUserDaoImpl.getSysOpUserByOperId(RejectedOperNo))
				.getOperBranchNo().equals(mca.getIssueBranchNo())) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"审批人不属于此机构");
		}

		// 检查机构是否激活
		if (IssueBranchDaoImpl.getIssueBranchByIssueBranchNo(
				mca.getIssueBranchNo()).getStatus() == IssueBranch.ISSUEBRANCH_STATUS_NOTACTIVATED)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"此机构被锁定");

		if (RejectedReason == null || RejectedReason.equals(""))
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"拒绝原因不可为空");

		// 赋值后提交审批结果
		mca.setStatus(MakeCardApproval.STATUS_REJECT);// 状态
		MakeCardApprovalDaoImpl.updateMakeCardApproval(mca);
		// 插入一条状态迁移明细
		MakeCardActionDetail makeCardActionDetail = new MakeCardActionDetail();
		makeCardActionDetail.setMakeCardApplyForId(mca.getMakeCardApplyForId());
		makeCardActionDetail.setActionTime(PCPSCommonDaoImpl
				.getDatabaseSysTime());
		makeCardActionDetail.setOperId(RejectedOperNo);
		makeCardActionDetail
				.setActionType(MakeCardActionDetail.MAKECARDACTIONDETAIL_ACTIONTYPE_FINALJUDGMENNOPASS);
		makeCardActionDetail
				.setInitialTranStatus(MakeCardApproval.STATUS_SALESAUTH);
		makeCardActionDetail.setEndTranStatus(MakeCardApproval.STATUS_REJECT);
		makeCardActionDetail.setRemark(RejectedReason);
		MakeCardActionDetailDaoImpl
				.insertMakeCardActionDetail(makeCardActionDetail);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "制卡申请财务拒绝", "InitialInStockOrderId:"
						+ makeCardActionDetail.getMakeCardApplyForId());
	}

	@Override
	public void updateMakeCardApproval(PCPSWebInput webInput,
			MakeCardApproval mac) throws PCPSApplicationException {
		if (webInput == null) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"登录信息未找到！");
		}
		if (mac == null || mac.getMakeCardApplyForId() == null) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待修改制卡申请信息未找到！");
		}
		MakeCardApproval macupdate = MakeCardApprovalDaoImpl
				.getMakeCardApprovalByMakeCardApplyForIdForUpdate(mac
						.getMakeCardApplyForId());
		if (macupdate == null) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待修改制卡申请信息未找到！");
		}
		// 判断是否为申请状态
		if (!MakeCardApproval.STATUS_APPROVAL.equals(macupdate.getStatus())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"制卡单状态必须为申请状态！");
		}
		// 制卡原因非空
		if (mac.getReason() == null)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"未提交制卡原因");
		macupdate.setReason(mac.getReason());
		// 有效期不可<=0
		if (mac.getExpired().intValue() <= 0)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"有效期异常");
		macupdate.setExpired(mac.getExpired());
		if (PCPSUtil.isEmpty(mac.getExpiredUnit())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"有效期单位不能为空！");
		}
		macupdate.setExpiredUnit(mac.getExpiredUnit());
		if (PCPSUtil.isEmpty(mac.getExpiredAlgorithm())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"有效期算法不能为空！");
		}
		macupdate.setExpiredAlgorithm(mac.getExpiredAlgorithm());
		// 根据操作员号取得操作员信息，对比branchno，验证是否发卡商，操作员是否此发卡商机构所属
		SysOpUser oper = SysOpUserDaoImpl.getSysOpUserByOperId(macupdate
				.getOperId());
		if (!webInput.getOperId().equals(oper.getOperId())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"制卡单修改必须和申请人一致!");
		}
		String issueBranchNo = oper.getOperBranchNo();
		if (mac.getCardCount() > 10000) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"制卡数量最大10000张！");
		}
		macupdate.setCardCount(mac.getCardCount());
		if (!oper.getOperBranchNo().equals(issueBranchNo)) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"操作员不属于此发卡商机构");
		}
		if (PCPSUtil.branchDistinguish(issueBranchNo) != PCPSUtil.SysBranch_IssueBranch) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"操作员所在不是发卡商机构");
		}
		IssueBranch issbrch = IssueBranchDaoImpl
				.getIssueBranchByIssueBranchNo(issueBranchNo);
		if (issbrch == null
				|| issbrch.getStatus() == IssueBranch.ISSUEBRANCH_STATUS_NOTACTIVATED)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"操作员所在机构不存在或被锁定");
		if (PCPSUtil.isEmpty(mac.getHintInfo())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"提示信息不能为空！");
		}
		macupdate.setHintInfo(mac.getHintInfo());

		// 取当前时间为申请时间
		macupdate.setLastModifyTime(PCPSCommonDaoImpl.getDatabaseSysTime());

		// 插入记录
		MakeCardApprovalDaoImpl.updateMakeCardApproval(macupdate);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "修改制卡申请", "InitialInStockOrderId:"
						+ mac.getMakeCardApplyForId());
	}

	@Override
	public void deleteMakeCardApprovalByID(PCPSWebInput webInput,
			MakeCardApproval mac) throws PCPSApplicationException {
		if (webInput == null) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"登录信息未找到！");
		}
		if (mac == null || mac.getMakeCardApplyForId() == null) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待修改制卡申请信息未找到！");
		}

		MakeCardApproval macupdate = MakeCardApprovalDaoImpl
				.getMakeCardApprovalByMakeCardApplyForId(mac
						.getMakeCardApplyForId());
		if (macupdate == null) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待修改制卡申请信息未找到！");
		}

		// 根据操作员号取得操作员信息，对比branchno，验证是否发卡商，操作员是否此发卡商机构所属
		SysOpUser oper = SysOpUserDaoImpl.getSysOpUserByOperId(macupdate
				.getOperId());
		if (!webInput.getOperId().equals(oper.getOperId())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"制卡单修改必须和申请人一致!");
		}

		// 判断是否为申请状态
		if (!MakeCardApproval.STATUS_APPROVAL.equals(macupdate.getStatus())) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"制卡单状态必须为申请状态！");
		}

		// 删除制卡申请迁移信息
		MakeCardActionDetailDaoImpl.deleteMakeCardActionDetail(macupdate.getMakeCardApplyForId());
		// 删除制卡申请单
		MakeCardApprovalDaoImpl.delete(macupdate);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Delete, "删除制卡申请", "InitialInStockOrderId:"
						+ mac.getMakeCardApplyForId());
	}
}
